package com.ruoyi.odorservice.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.odorservice.entity.EnterpriseIndustry;
import com.ruoyi.odorservice.entity.Industry;
import com.ruoyi.odorservice.entity.Material;
import com.ruoyi.odorservice.entity.SourceEnterprise;
import com.ruoyi.odorservice.model.bean.IndustryInfoListBean;
import com.ruoyi.odorservice.model.vo.IndustryCraftOdorVo;
import com.ruoyi.odorservice.model.vo.IndustryListVo;
import com.ruoyi.odorservice.service.EnterpriseIndustryService;
import com.ruoyi.odorservice.service.IndustryService;
import com.ruoyi.odorservice.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/industry")
@Api(tags = "行业信息")
@AllArgsConstructor
public class IndustryController {

    private IndustryService industryService;
    private EnterpriseIndustryService enterpriseIndustryService;
    private MaterialService materialService;

    @GetMapping("/getIndustryCraftOdor")
    @ApiOperation(value = "获取工艺下的气味轮廓图数据",httpMethod = "GET", response = IndustryCraftOdorVo.class,notes = "弃用", hidden = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "craftId" , value = "工艺id", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "industryId" , value = "行业id", paramType = "query", dataType = "Integer", required = true)
    })
    public Result<List<IndustryCraftOdorVo>> getIndustryCraftOdorInfo(Integer craftId, Integer industryId) {
        return industryService.getIndustryInfo(craftId, industryId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取行业列表",httpMethod = "GET" ,notes = "弃用", hidden = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page" , value = "页码", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "limit" , value = "条数", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "industryName" , value = "行业名称", paramType = "query", dataType = "String", required = false)
    })
    public Result<IndustryListVo> industryList(Integer page, Integer limit, String industryName) {
        if(page == null) page = 1;
        if(limit == null) limit = 10000000;
        return industryService.industryList(page, limit, industryName);
    }

    @PostMapping("/addIndustry")
    @ApiOperation(value = "添加行业",httpMethod = "POST" ,notes = "弃用", hidden = true)
    public Result<Boolean> addIndustry(@RequestBody Industry industry) {
        if(StringUtils.isEmpty(industry.getIndustryName())) return Result.fail("行业名称不能为空");
        return industryService.addIndustry(industry);
    }
    @PostMapping("/updateIndustry")
    @ApiOperation(value = "修改行业信息",httpMethod = "POST" ,notes = "弃用", hidden = true)
    public Result<Boolean> updateIndustry(@RequestBody Industry industry) {
        if(industry.getId() == null) return Result.fail("id不能为空");
        if(StringUtils.isEmpty(industry.getIndustryName())) return Result.fail("行业名称不能为空");
        return industryService.updateIndustry(industry);
    }

    @GetMapping("/industryOptions")
    @ApiOperation(value = "查询行业字典", httpMethod = "GET")
    public Result<List<String>> getIndustries() {
        List<EnterpriseIndustry> industries = enterpriseIndustryService.list(new LambdaQueryWrapper<EnterpriseIndustry>()
                .select(EnterpriseIndustry::getIndustryName));
        return Result.ok(industries.stream().map(EnterpriseIndustry::getIndustryName).distinct().collect(Collectors.toList()));
    }

    @GetMapping("/listByIndustry")
    @ApiOperation(value = "气味词典&轮廓图（行业->工艺->气味）", httpMethod = "GET", response = SourceEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "industry", value = "行业名称", paramType = "query", dataType = "String", required = true)
    })
    public Result<List<EnterpriseIndustry>> listByIndustry(@RequestParam(value = "industry") String industry) {
        List<EnterpriseIndustry> list = enterpriseIndustryService.list(new LambdaQueryWrapper<EnterpriseIndustry>()
                .eq(StrUtil.isNotEmpty(industry),EnterpriseIndustry::getIndustryName,industry));
        return Result.ok(list);
    }

    @PostMapping("/addEnterpriseIndustry")
    @ApiOperation(value = "新增企业所属行业", httpMethod = "POST", response = Boolean.class)
    public Result<Boolean> addEnterpriseIndustry(@RequestBody EnterpriseIndustry enterpriseIndustry) {
        if (StringUtils.isEmpty(enterpriseIndustry.getIndustryName())) return Result.fail("行业名称不能为空");
        return Result.ok(enterpriseIndustryService.save(enterpriseIndustry));
    }

    @PutMapping("/updateEnterpriseIndustry")
    @ApiOperation(value = "修改企业所属行业", httpMethod = "PUT", response = Boolean.class)
    @ApiImplicitParam(name = "id", value = "企业所属行业ID", paramType = "path", dataType = "Integer", required = true)
    public Result<Boolean> updateEnterpriseIndustry(@PathVariable Integer id, @RequestBody EnterpriseIndustry enterpriseIndustry) {
        if (id == null) return Result.fail("id不能为空");
        enterpriseIndustry.setId(id);
        return Result.ok(enterpriseIndustryService.updateById(enterpriseIndustry));
    }

    @GetMapping("/listOdorsByIndustry")
    @ApiOperation(value = "根据行业查询气味", httpMethod = "GET", response = Material.class)
    @ApiImplicitParam(name = "industry", value = "行业名称", paramType = "query", dataType = "String", required = true)
    public Result<List<Material>> listOdorsByIndustry(@RequestParam(value = "industry") String industry) {
        List<EnterpriseIndustry> list = enterpriseIndustryService.list(new LambdaQueryWrapper<EnterpriseIndustry>()
                .eq(StrUtil.isNotEmpty(industry), EnterpriseIndustry::getIndustryName, industry));
        List<String> odorNames = list.stream().map(EnterpriseIndustry::getOdorName).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        for (String name : odorNames) {
            queryWrapper.or(wrapper -> wrapper.like(Material::getOdorCharacteristics, name));
        }
        List<Material> materials = materialService.list(queryWrapper);
        return Result.ok(materials);
    }

}
